package com.junzhiit.nengji.cms.web.vo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import com.bainaeco.framework.base.BaseObject;

public class TreeNodeVO extends BaseObject {

	private static final long serialVersionUID = 1L;

	private String id;

	private String name;

	private String label;

	private MenuVO data;

	private String parentId;

	private Integer isSelect;

	private Integer sort;

	private List<TreeNodeVO> children = new ArrayList<TreeNodeVO>();

	public void addChild(TreeNodeVO menu) {
		children.add(menu);
	}

	public void sortChild() {
		if (children.size() != 0) {
			Collections.sort(children, new SortComparator());
			for (int i = 0, size = children.size(); i < size; i++) {
				children.get(i).sortChild();
			}
		}
	}

	public class SortComparator implements Comparator<TreeNodeVO> {
		public int compare(TreeNodeVO o1, TreeNodeVO o2) {
			return o1.getSort() < o2.getSort() ? -1 : (o1.getSort() == o2.getSort() ? 0 : 1);
		}
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public MenuVO getData() {
		return data;
	}

	public void setData(MenuVO data) {
		this.data = data;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public Integer getSort() {
		return sort;
	}

	public void setSort(Integer sort) {
		this.sort = sort;
	}

	public List<TreeNodeVO> getChildren() {
		return children;
	}

	public void setChildren(List<TreeNodeVO> children) {
		this.children = children;
	}

	public Integer getIsSelect() {
		return isSelect;
	}

	public void setIsSelect(Integer isSelect) {
		this.isSelect = isSelect;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public void sortChildren() {  
		// 对本层节点进行排序  
		// 可根据不同的排序属性，传入不同的比较器，这里传入ID比较器  
		Collections.sort(this.getChildren(), new NodeIDComparator());  
		// 对每个节点的下一层节点进行排序  
		for (Iterator<TreeNodeVO> it = children.iterator(); it.hasNext();) {  
			it.next().sortChildren();  
		}  
	}
	
	class NodeIDComparator implements Comparator<TreeNodeVO> {  
		// 按照节点编号比较  
		@Override
		public int compare(TreeNodeVO o1, TreeNodeVO o2) {
			int j1 = o1.getSort();  
			int j2 = o2.getSort();  
			return (j1 < j2 ? -1 : (j1 == j2 ? 0 : 1)); 
		}   
	}
	
}
